package com.scrollpickerview;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 根据ScrollPickerView 构造的 时间选择器
 *
 * @author Yobert Jomi
 *         className TimePicker
 *         created at  2016/10/24  15:14
 */
public class TimePicker
{
	/**
	 * 结果回调
	 */
	public interface ResultCallBack
	{
		void choosed(String year, String month, String day);
	}

	private Context context;
	private ResultCallBack resultCallBack;

	public TimePicker(Context context, ResultCallBack resultCallBack)
	{
		this.context = context;
		this.resultCallBack = resultCallBack;
	}

	public void show()
	{
		final DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();
		decimalFormat.applyPattern("00");
		final ViewGroup viewGroup = null;
		View view = LayoutInflater.from(context).inflate(R.layout.inflate_time_picker, viewGroup, false);
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		view.setLayoutParams(params);
		final AlertDialog mDialog = new AlertDialog.Builder(context).create();
		TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
		TextView tv_ensure = (TextView) view.findViewById(R.id.tv_ensure);
		final ScrollPickerView scrollPickerViewYear = (ScrollPickerView) view.findViewById(R.id.scrollPickerViewYear);
		final ScrollPickerView scrollPickerViewMonth = (ScrollPickerView) view.findViewById(R.id.scrollPickerViewMonth);
		final ScrollPickerView scrollPickerViewDay = (ScrollPickerView) view.findViewById(R.id.scrollPickerViewDay);
		tv_cancle.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mDialog.dismiss();
			}
		});
		tv_ensure.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (null != resultCallBack)
				{
					try
					{
						resultCallBack.choosed(decimalFormat.format(Long.parseLong(scrollPickerViewYear.getSelectedItem().replace("年", ""))),
								decimalFormat.format(Long.parseLong(scrollPickerViewMonth.getSelectedItem().replace("月", ""))),
								decimalFormat.format(Long.parseLong(scrollPickerViewDay.getSelectedItem().replace("日", ""))));
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		});
		Calendar calendar = Calendar.getInstance();

		initPickerViewData(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), scrollPickerViewYear,
				scrollPickerViewMonth, scrollPickerViewDay);
		mDialog.show();

		Window window = mDialog.getWindow();
		if (window != null)
		{
			WindowManager.LayoutParams layoutParams = window.getAttributes();
			//		layoutParams.width = ScreenUtils.getScreenWidth(activity);
			layoutParams.width = 1080;
			window.setAttributes(layoutParams);
			window.setGravity(Gravity.BOTTOM);

			window.setBackgroundDrawable(new ColorDrawable(0));
			window.setContentView(view);
			mDialog.setCancelable(true);
			mDialog.setCanceledOnTouchOutside(false);
		}
	}

	/**
	 * 初始化pickerview 的数据
	 */
	private void initPickerViewData(int ORIGIN_YEAR, int ORIGIN_MONTH, int ORIGIN_DAY, final ScrollPickerView scrollPickerViewYear,
			final ScrollPickerView scrollPickerViewMonth, final ScrollPickerView scrollPickerViewDay)
	{
		// 可选择的年份，从1900到现在
		final String[] YEARS;
		{
			ArrayList<String> list = new ArrayList<>();
			int curYear = DateUtil.getInstance().getYear();
			for (int i = 1900; i <= curYear + 1; i++)
			{
				list.add(i + "年");
			}
			YEARS = list.toArray(new String[list.size()]);
		}
		// 月份
		final String[] MONTHS = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };

		// 设置数据
		scrollPickerViewYear.setDataList(new ArrayList<>(Arrays.asList(YEARS)));
		scrollPickerViewMonth.setDataList(new ArrayList<>(Arrays.asList(MONTHS)));
		scrollPickerViewDay.setDataList(DateUtil.getInstance().getMonthDaysArray(ORIGIN_YEAR, ORIGIN_MONTH));

		// 设置初始值
		scrollPickerViewYear.setSelectedPosition(scrollPickerViewYear.getData().indexOf(String.valueOf(ORIGIN_YEAR)));
		scrollPickerViewMonth.setSelectedPosition(ORIGIN_MONTH - 1);
		scrollPickerViewDay.setSelectedPosition(ORIGIN_DAY - 1);

		// 监听年份更改
		scrollPickerViewYear.setOnSelectedListener(new ScrollPickerView.OnSelectedListener()
		{
			@Override
			public void onSelected(List<String> data, int position)
			{
				int month = Integer.parseInt(scrollPickerViewMonth.getSelectedItem().replace("月", ""));
				// ２月份,更新天数
				if (month == 2)
				{
					changeMonthDays(scrollPickerViewYear, scrollPickerViewMonth, scrollPickerViewDay);
				}
			}
		});

		// 监听月份更改
		scrollPickerViewMonth.setOnSelectedListener(new ScrollPickerView.OnSelectedListener()
		{
			@Override
			public void onSelected(List<String> data, int position)
			{
				changeMonthDays(scrollPickerViewYear, scrollPickerViewMonth, scrollPickerViewDay);
			}
		});
	}

	// 更新天数
	private void changeMonthDays(ScrollPickerView scrollPickerViewYear, final ScrollPickerView scrollPickerViewMonth, ScrollPickerView scrollPickerViewDay)
	{
		int year = Integer.parseInt(scrollPickerViewYear.getSelectedItem().replace("年", ""));
		int month = Integer.parseInt(scrollPickerViewMonth.getSelectedItem().replace("月", ""));
		int day = Integer.parseInt(scrollPickerViewDay.getSelectedItem().replace("日", ""));
		List<String> dayList = DateUtil.getInstance().getMonthDaysArray(year, month);
		if (scrollPickerViewDay.getData().size() != dayList.size())
			scrollPickerViewDay.setDataList(dayList);//如果天数有变化，重新设置
		scrollPickerViewDay.setSelectedPosition(day > dayList.size() ? dayList.size() - 1 : day - 1);
	}
}
